package com.winai.launcher.collector;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.bean.requestBean.UseInfoBean;
import com.winai.launcher.manager.DeviceInfoManager;
import com.winai.launcher.manager.PageNameManager;
import com.winai.launcher.utils.BaseUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 作者：康少
 * 时间：2020/4/13 0013
 * 说明：activity页面行为收集
 */
public class PageCollector {
    private static final String TAG = "PageCollector";
    private String mPageName;
    private String mTitle;
    private String mTag;
    private static PageCollector mInstance;

    /**
     * 单例
     *
     * @return 单例
     */
    public static PageCollector getInstance() {
        if (null == mInstance) {
            mInstance = new PageCollector();
        }
        return mInstance;
    }

    /**
     * activity打开
     *
     * @param pageId 标识
     * @param title  标题
     * @param tag    备注
     * @param type   activity 类型  0 默认类型 1 从后台返回前台  2 从 从前台到后台
     * @return 动作元素
     */
    public UseInfoBean pageOpenInfoGenerated(Context context, String pageId, String title, String tag, int type, String ip) {
        if (null == pageId && null == title && null == tag) {
            return null;
        }
        UseInfoBean mUseInfoBean = new UseInfoBean();
        mUseInfoBean.setEquipmentId(BaseUtils.getDeviceId());
        mUseInfoBean.setIp(ip);
        mUseInfoBean.setSerialNumber(DeviceInfoManager.getInstance(context).getDevice().getDeviceSNCode());
        mUseInfoBean.setUserAccount(DeviceInfoManager.getInstance(context).getDevice().getUserAccount());
        mUseInfoBean.setUseTime(BaseUtils.timeStamp2Date(BaseUtils.getInitTime(), "yyyy-MM-dd HH:mm:ss"));
        if (null != pageId) {
            pageId = pageId.substring(pageId.lastIndexOf(".") + 1, pageId.indexOf("@"));
            mUseInfoBean.setClazz(pageId);
        }
        mUseInfoBean.setPage(PageNameManager.getPageName(pageId));
        mUseInfoBean.setEvent("in");
        mUseInfoBean.setOperationType("进入");
        return mUseInfoBean;
    }

    public String getPageName() {
        return mPageName;
    }

    public String getPageTitle() {
        return mTitle;
    }

    public String getPageTag() {
        return mTag;
    }

    /**
     * activity关闭
     *
     * @param pageId 标识
     * @param title  标题
     * @param tag    备注
     * @param type   activity 类型  0 默认类型 1 从后台返回前台  2 从 从前台到后台
     * @return 动作元素
     */
    public UseInfoBean pageCloseInfoGenerated(Context context, String pageId, String title, String tag, int type, String ip) {
        if (null == pageId && null == title && null == tag) {
            return null;
        }
        UseInfoBean mUseInfoBean = new UseInfoBean();
        mUseInfoBean.setIp(ip);
        mUseInfoBean.setEquipmentId(BaseUtils.getDeviceId());
        mUseInfoBean.setSerialNumber(DeviceInfoManager.getInstance(context).getDevice().getDeviceSNCode());
        mUseInfoBean.setUserAccount(DeviceInfoManager.getInstance(context).getDevice().getUserAccount());
        mUseInfoBean.setUseTime(BaseUtils.timeStamp2Date(BaseUtils.getInitTime(), "yyyy-MM-dd HH:mm:ss"));
        if (null != pageId) {
            pageId = pageId.substring(pageId.lastIndexOf(".") + 1, pageId.indexOf("@"));
            mUseInfoBean.setClazz(pageId);
        }
        mUseInfoBean.setPage(PageNameManager.getPageName(pageId));
        mUseInfoBean.setEvent("leave");
        mUseInfoBean.setOperationType("离开");
//        mUseInfoBean.setDuration();
        return mUseInfoBean;
    }

    /**
     * app关闭动作搜集
     *
     * @param context
     * @return 动作元素
     */
    public UseInfoBean appCloseEventGeneration(Context context, String ip) {
        try {
            String appName = context.getPackageName();
            UseInfoBean mUseInfoBean = new UseInfoBean();
            mUseInfoBean.setIp(ip);
            mUseInfoBean.setEquipmentId(BaseUtils.getDeviceId());
            mUseInfoBean.setSerialNumber(DeviceInfoManager.getInstance(context).getDevice().getDeviceSNCode());
            mUseInfoBean.setUserAccount(DeviceInfoManager.getInstance(context).getDevice().getUserAccount());
            mUseInfoBean.setUseTime(BaseUtils.timeStamp2Date(BaseUtils.getInitTime(), "yyyy-MM-dd HH:mm:ss"));
            mUseInfoBean.setClazz(appName);
            mUseInfoBean.setPage(appName);
            mUseInfoBean.setEvent("quit");
            mUseInfoBean.setOperationType("退出");
            return mUseInfoBean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private PageCollector() {

    }

}
